package com.product.controller;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @USER: songxusheng
 * @DATE: 2024/7/11 10:34
 * @DESCRIPTION:
 * @PROJECT_NAME: assembles
 */
@RestController
@RequestMapping("/send")
public class TestSendMessageController {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    final String TOPIC = "springboot-mq";

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 测试发送简单消息
     */
    @GetMapping("sendSimpleMessage")
    public void sendSimpleMessage(){
        SendResult sendResult = rocketMQTemplate.syncSend(TOPIC, "这是一条简单消息");
        logger.info(sendResult.getMsgId());
        logger.info(String.valueOf(sendResult.getSendStatus()));

    }
    /**
     * 测试发送异步消息
     */
    @GetMapping("sendAsyncMessage")
    public void sendAsyncMessage(){
        rocketMQTemplate.asyncSend(TOPIC, "发送一个异步消息", new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                logger.info("消费成功");
            }

            @Override
            public void onException(Throwable throwable) {
                logger.info("消费失败");
            }
        });
    }
    /**
     * 测试发送延迟消息
     */
    @GetMapping("sendDelayMessage")
    public void sendDelayMessage(){
        SendResult sendResult = rocketMQTemplate.syncSend(TOPIC, MessageBuilder.withPayload("这是一条延迟消息").build(), 10000L, 1);
        logger.info(sendResult.getMsgId());
        logger.info(String.valueOf(sendResult.getSendStatus()));
    }

}
